--
-- Author: shunguo.chen
-- Date: 2016-08-09 13:53:57
--
-- MallRender

local ClassRef = app.mvc.createRenderClass(...)

local NUM_LIMIT_STR_1 = L("demondShop_buynumlimit_oneday")
local NUM_LIMIT_STR_2 = L("demondShop_buynumlimit_lifttime")
local NUM_LIMIT_STR_3 = L("demondShop_buynumlimit_oneweek")
local DISCOUNT_STR = L("demondShop_discount")

--data = MallVo
function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.MallNode))
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	-- dumpNodeChildren(self, "MallRender")

	local panel = self._ui.Panel
	self:setContentSize(panel:getContentSize())
	display.uiAddClick(panel, function()
		if not self:isSelected() and self._data then
			executeFunc(self._onBtnClick, self, self._data)
		end
	end, true, 0.01)

	self._priceIcon = self._ui["Panel/price/icon"]
	self._priceText = self._ui["Panel/price/num_1"]
	self._realPriceText = self._ui["Panel/price/num_2"]
	self._priceTextPosX = self._priceText:getPositionX()
	self._realPriceInitPosX = self._realPriceText:getPositionX()

	self._rushIconPosX = self._ui.icon_all:getPositionX()
	self._limitIconPosX = self._ui.icon_limit:getPositionX()

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		local data = self._data
		local itemData = data:getShowItemData()
		self._ui.icon:loadTexture(itemData:getIcon())
		self._ui.name:setString(data:getName())
		self._ui.name:setTextColor(Enums.QualityColor[itemData:getQuality()])

		self:setIsNew()
		self:setDiscount()
		self:setLimit()
	end
end

function ClassRef:setIsNew()
	if self._refreshNewAct then 
		self:stopAction(self._refreshNewAct)
		self._refreshNewAct = nil
	end

	local isNew, refreshGap = self._data:isNew()
	local isLifeLimitOver = self._data:isLifeLimitOver()
	self._ui.tips_new:setVisible(isNew and not isLifeLimitOver)

	if refreshGap then
		self._refreshNewAct = self:performWithDelay(function()
				self._refreshNewAct = nil
				self:setDiscount()
			end, refreshGap)
	end
end

function ClassRef:setDiscount()
	if self._refreshDiscountAct then 
		self:stopAction(self._refreshDiscountAct)
		self._refreshDiscountAct = nil
	end

	local data = self._data
	local isDiscount, refreshGap = data:isDiscount()

	self._ui.tips_agio:setVisible(isDiscount)
	self._ui.bg_agio:setVisible(isDiscount)
	self._priceIcon:loadTexture(data:getDaibiSmallIcon())
	self._priceText:setVisible(isDiscount)

	self._realPriceText:setString(data.price)
	if isDiscount then
		self._realPriceText:setPositionX(self._realPriceInitPosX)
		self._ui.tips_agio:setString(L("discount_" .. data.discount))
		self._priceText:setString(data.srcPrice)
		display.doLayout(self._priceText)
        display.doLayout(self._realPriceText)
		display.setNodesLeft(nil, self._priceIcon, self._priceText, self._realPriceText)
	else
		self._realPriceText:setPositionX(self._priceTextPosX)
		display.setNodesLeft(nil, self._priceIcon, self._realPriceText)
	end

	if refreshGap then
		self._refreshDiscountAct = self:performWithDelay(function()
				self._refreshDiscountAct = nil
				self:setDiscount()
			end, refreshGap)
	end
end

function ClassRef:setLimit()
	-- num limit
	local data = self._data
	local numTypeId = data.numTypeId
	local limitText = self._ui.text
	if data.numTypeId == 0 then
		limitText:setVisible(false)
	else
		local str
		if numTypeId == 1 then
			str = NUM_LIMIT_STR_1
		elseif numTypeId == 2 then
			str = NUM_LIMIT_STR_2
		elseif numTypeId == 3 then
			str = NUM_LIMIT_STR_3
		end
		limitText:setVisible(true)
		limitText:setString(string.format(str, data.leftNum, data.limitNum))
	end

	-- rush buy / time limit
	local isRushBuy = data:isRushBuy()
	self._ui.icon_all:setVisible(isRushBuy)
	if data:isTimeLimit() then
		self._ui.icon_limit:setVisible(true)
		if isRushBuy then
			self._ui.icon_all:setPositionX(self._rushIconPosX)
		end
	else
		self._ui.icon_limit:setVisible(false)
		if isRushBuy then
			self._ui.icon_all:setPositionX(self._limitIconPosX)
		end
	end
end

function ClassRef:isSelected()
	return self._ui.selected:isVisible()
end
function ClassRef:setSelected(b)
	self._ui.selected:setVisible(b)
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

return ClassRef
